﻿/*
创建人：longb
日期：2022-07-06
描述：故检提交单js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSGJCheckSubmitBillHandler.ashx",
    comboxUrl:getRootPath() + "/Ashx/ZsHandler/ZSTestRunNoticeBillHandler.ashx",//combox下拉-调用后台的URL
    comboxAction:"GetMfgorder",//combox下拉-调用后台的具体方法
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        var Initid = 0;
        var data = [{ id: 0, text: '待提交' }, { id: 1, text: '审核中' },  { id: 2, text: '审核完' }, { id: 3, text: '已完成'}];
        $('#Status').combobox({
            data: data,
            valueField: 'id',
            textField: 'text',
            onSelect: function (record) {
            }
        });
        $('.ProductType').combobox({
            data: [{ id: "0", text: '批产' }, { id: "1", text: '附加' }, { id: "2", text: '新机'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#Status').combobox('setValue', Initid);
        $('#ISQUALIFIED').combobox({
            data: [{ id: "0", text: '否' }, { id: "1", text: '是' }],
            valueField: 'id',
            textField: 'text'
        });
        $('#mfgCombox').selectCombox({
            search: function () { },
            cancel: function () {$('#MFGORDERID').val(''); $('#ATTRIBUTEVALUE').textbox('setValue','');},
            selected: function (val, text,record){
                $('#MFGORDERID').val(val);
                $('#txtCombox').textbox('setValue',text);
                $('#ATTRIBUTEVALUE').textbox('setValue', record.ATTRIBUTEVALUE);
                $('#PRODUCTFAMILYNAME').textbox('setValue', record.PRODUCTFAMILYNAME);//型号
                $('#divComboxTxt').show();
                $('#divComboxSelect').hide();
                $('#divComboxSearch').show();
                $('#divComboxClear').hide();
            }
        });
        //任务单信息
        $("#GJCheckInfoTable").datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            rownumbers: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ZSGJCHECKSUBMITBILLID', width: 120, checkbox: true },
                 { field: 'ZSGJCHECKSUBMITBILLNO', title: '故检提交单号', width: 130 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
                 { field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
		         { field: 'PRODUCTFAMILYNAME', title: '型号', width: 130 }, //--
                 {field: 'PRODUCTTYPE', title: '产品类别', width: 80, formatter: function (value, row, index) {
                    if(value==0){return"批产";}else if(value==1){return "附加";}else if(value==2){return "附加";}
                 } },
                 { field: 'GJCHECKFINISHDATE', title: '故检完成日期', width: 90 },
                 { field: 'ISQUALIFIED', title: '是否合格', width: 80,formatter: function (value, row, index) {if(value==0){return "否"}else{return "是"}}  },
                 { field: 'STATUS', title: '状态', width: 80,  },
                 { field: 'CREATENAME', title: '创建人', width: 80, },
                 { field: 'CREATEDATE', title: '创建时间', width: 130 },
                 { field: 'SUBMITNAME', title: '提交人', width: 80,  },
                 { field: 'SUBMITDATE', title: '提交时间', width: 130 },
                 { field: 'AUDITNAME', title: '审核人', width: 80,},
                 { field: 'AUDITDATE', title: '审核时间', width: 130 }
                 
	           ]],
            onSelect: function (index, row) {
                row["MFGORDERNO"] = row.MFGORDERID;
                $('#EditorForm').form('load', row);
                if(!$('#btnComboxSearch').hasClass('l-btn-disabled')){//防止连续行点击，查询按钮事件失效
                    $('#txtCombox').combobox('disable');
                    $('#btnComboxSearch').linkbutton('disable');
                }
                $("#GJCheckInfoTable").datagrid("uncheckAll").datagrid("checkRow",index);//单选行，去掉同时勾选
            },
            onCheckAll:function(rows){
                $("#GJCheckInfoTable").datagrid("clearSelections");
                 $('#EditorForm').form("clear");
            },
            onCheck:function(rowIndex,rowData){
                var checkRows= $("#GJCheckInfoTable").datagrid("getChecked");
                var selectRow= $("#GJCheckInfoTable").datagrid("getSelected");
                if(checkRows.length>1){
                    $("#GJCheckInfoTable").datagrid("clearSelections");
                     $('#EditorForm').form("clear");
                }
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetGJCheckSubmitBill();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetGJCheckSubmitBill();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        page.GetGJCheckSubmitBill();
        page.CreateGJCheckSubmitBill();
    },
    //查询试车通知单信息
    GetGJCheckSubmitBill: function () {
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetGJCheckSubmitBill",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                //console.info(res);
                page.pagetotal = res.RowCount;
                $('#GJCheckInfoTable').datagrid('loadData', res.DBTable);
            }
        });
    },
    //保存试车通知单信息
    SaveGJCheckSubmitBill: function () {
        var row = $('#GJCheckInfoTable').datagrid('getSelected');
        if (row != null && row.STATUS != "待提交") { displayMessage(row.STATUS + "的提交单，不能保存", false); return; }
        var frdata = $('#EditorForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "SaveGJCheckSubmitBill",
            data: JSON.stringify([data])
        };
        if (data.MFGORDERID == "") { displayMessage("订单号不能为空，请选择", false); return; }
        if (data.PRODUCTTYPE == "") { displayMessage("产品类型不能为空，请选择", false); return; }
        if (data.GJCHECKFINISHDATE == "") { displayMessage("故检完成日期不能为空，请选择", false); return; }
        if (data.ISQUALIFIED == "") { displayMessage("是否合格不能为空，请选择", false); return; }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetGJCheckSubmitBill();
                page.CreateGJCheckSubmitBill();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //新建故检单信息
    CreateGJCheckSubmitBill: function () {
        $('#EditorForm').form("clear");
        $('#txtCombox').combobox('enable');
        $('#btnComboxSearch').linkbutton('enable');
        $('#GJCheckInfoTable').datagrid('unselectAll');
        clearMessage();
    },
    //故检单--提交
    SubmitGJCheckSubmitBill: function () {
        page.GJCheckSubmitBill("SubmitGJCheckSubmitBill");
    },
    //故检单--完成
    FinishGJCheckSubmitBill: function () {
        page.GJCheckSubmitBill("FinishGJCheckSubmitBill");
    },
   
    GJCheckSubmitBill: function (action) {
        var ids = [];
        var rows = $('#GJCheckInfoTable').datagrid("getChecked");
        $.each(rows, function (index, item) {
            ids.push(item.ZSGJCHECKSUBMITBILLID);
        });
        if (ids.length == 0) { displayMessage("请勾选记录再进行操作", false); return; }
        if(action=="SubmitGJCheckSubmitBill"&&rows[0].STATUS!="待提交"){displayMessage("所选提交单"+rows[0].STATUS+",不能再提交",false);return;}
        if(action=="FinishGJCheckSubmitBill"&&rows[0].STATUS!="审核完"){displayMessage("所选提交单"+rows[0].STATUS+",不能进行完成",false);return;}
        var param = {
            action: action,
            ids: ids.join(',')
        };
        if (ids.length == 0) { displayMessage("请勾选记录再进行操作", false); return; }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetGJCheckSubmitBill();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },

    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});